//
//  GNDiscoverSeacherView.swift
//  SinaWeChat
//
//  Created by mac on 15/12/5.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class GNDiscoverSeacherView: UIView ,UITextFieldDelegate{

    
    @IBOutlet weak var textField: UITextField!
    
    @IBOutlet weak var cancleButton: UIButton!
    
    @IBOutlet weak var rightcCons: NSLayoutConstraint!
    
    //从xib里面加载 View
    
    class func searchView ()->GNDiscoverSeacherView{
        
        return NSBundle.mainBundle().loadNibNamed("GNDiscoverSeacherView", owner: nil, options: nil).last! as! GNDiscoverSeacherView
    }
    
    //与代码创建控件有关
    
     override init(frame: CGRect) {
         super.init(frame: frame)
    }
    
    //从xib里面将线连接好后会调用这个方法
    override func awakeFromNib() {
        
        let leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        leftView.frame = CGRectMake(0, 0, frame.height, frame.height)
        
        //设置内容显示模式
        leftView.contentMode = .Center
        
        //设置leftView
        textField.leftView = leftView
        
        //指定其显示模式
        textField.leftViewMode = UITextFieldViewMode.Always
        
    }

     required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
         //fatalError("init(coder:) has not been implemented")
     }
    
    
    @IBAction func cancelButtonClick(sender: AnyObject) {
        
        //取消第一响应者
        textField.resignFirstResponder()
        
        //执行动画sel
        
        self.rightcCons.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textField.layoutIfNeeded()
        }
    }
    
    
    func textFieldDidBeginEditing(textField: UITextField) {
        
        
        //要执行的动画约束
        self.rightcCons.constant = self.cancleButton.frame.width
        
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.textField.layoutIfNeeded()
            
        }
    }
    
}





